package com.east.exception;
/**
 * 异常规则
 */
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpRequestMethodNotSupportedException;

import com.alibaba.fastjson.JSON;
import com.east.utils.ResultJson;
import com.east.utils.ResultUtil;

import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import net.sf.json.JSONException;
public class ExceptionUtil {
		public static  void exception(Logger logger,HttpServletResponse response,HttpServletRequest request,Exception e){
			System.out.println(e+"111111111111");
			if(e instanceof ArithmeticException ){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.ArithmeticException.getName());
				return;
			}else if(e instanceof ArrayIndexOutOfBoundsException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.ArrayIndexOutOfBoundsException.getName());
				return;
			}else if(e instanceof ArrayStoreException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.ArrayStoreException.getName());
				return;
			}else if(e instanceof ClassCastException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.ClassCastException.getName());
				return;
			}else if(e instanceof EOFException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.EOFException.getName());
				return;
			}else if(e instanceof FileNotFoundException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.FileNotFoundException.getName());
				return;
			}else if(e instanceof IllegalArgumentException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.IllegalArgumentException.getName());
				return;
			}else if(e instanceof IndexOutOfBoundsException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.IndexOutOfBoundsException.getName());
				return;
			}else if(e instanceof IOException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.IOException.getName());
				return;
			}else if(e instanceof NegativeArraySizeException){
				outError( logger,response, request,ResultEnums.error_500.getCode(), ExecptionMeaning.NegativeArraySizeException.getName());
				return;
			}else if(e instanceof NoSuchMethodException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.NoSuchMethodException.getName());
				return;
			}else if(e instanceof NullPointerException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.NullPointerException.getName());
				return;
			}else if(e instanceof NumberFormatException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.NumberFormatException.getName());
				return;
			}else if(e instanceof UnsupportedOperationException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.UnsupportedOperationException.getName());
				return;
			}else if(e instanceof SQLException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.SQLException.getName());
				return;
			}else if(e instanceof HttpRequestMethodNotSupportedException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.HttpRequestMethodNotSupportedException.getName());
				return;
			}else if(e instanceof BadSqlGrammarException){
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.BadSqlGrammarException.getName());
				return;
			}else if(e instanceof SignatureException){
				outError( logger,response,request, ResultEnums.error_403.getCode(), ExecptionMeaning.SignatureException.getName());
				return;
			}else if(e instanceof MalformedJwtException) {
				outError( logger,response,request, ResultEnums.error_403.getCode(), ExecptionMeaning.MalformedJwtException.getName());
				return;
			}else if(e instanceof JSONException) {
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.JSONException.getName());
				return;
			}else if(e instanceof UnauthorizedException) {
				outError( logger,response,request, ResultEnums.error_401.getCode(), ExecptionMeaning.UnauthorizedException.getName());
				return;
			}
			else if(e instanceof UnknownAccountException ) {
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.UnknownAccountException.getName());
				return;
			}
			else if(e instanceof UnauthenticatedException ) {
				outError( logger,response,request, ResultEnums.error_403.getCode(), ExecptionMeaning.UnauthenticatedException.getName());
				return;
			}
			else if(e instanceof DataIntegrityViolationException ) {
				outErrorDel( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.DataIntegrityViolationException.getName());
				return;
			}
			else {
				outError( logger,response,request, ResultEnums.error_500.getCode(), ExecptionMeaning.Exception.getName());
				return;
			}
			
			
		}
		public static void outError(Logger logger,HttpServletResponse response,HttpServletRequest request,Integer code,String msg) { 
			try {
				logger.error(msg);
				String json = JSON.toJSONString(new ResultJson(code, request.getServletPath(), msg));
				response.setCharacterEncoding("UTF-8");  
				response.setContentType("text/json");  
				ServletOutputStream os = response.getOutputStream();  
				os.write(json.getBytes("utf-8"));  			
			} catch (Exception e) {
				logger.error("【 异常】 {}", e);
			}
		 }  
		
		public static void outErrorDel(Logger logger,HttpServletResponse response,HttpServletRequest request,Integer code,String msg) { 
			try {
				logger.error(msg);
				String json = JSON.toJSONString( ResultUtil.error(500,msg,"删除失败"));
				response.setCharacterEncoding("UTF-8");  
				response.setContentType("text/json");  
				ServletOutputStream os = response.getOutputStream();  
				os.write(json.getBytes("utf-8"));  			
			} catch (Exception e) {
				logger.error("【 异常】 {}", e);
			}
		 }  
}
